/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.insightfullogic.java8.examples.chapter1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.function.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *
 * @author richard
 *
 * lambda 也得遵循 基本法;
 *
 *
 *
 *
 */
public class Chapter1 extends MusicChapter {



    /**
     *  map ( Function<T,R>
     * T  Artist 类型
     * R  返回 String类型
     *   )
     *
     * */
    public List<String> getNamesOfArtists_Lambda() {
        List<String> collect = super.artists.stream()
                .map(artist ->  artist.getName()   )
                .collect(   Collectors.toList()  );

       // System.out.println(collect.getClass().getName());
        return collect;
    }


    /**
     * 方法引用
     * */
    public List<String> getNamesOfArtists_MethodReference() {
        return artists.stream()
                      .map(Artist::getName)
                      .collect(Collectors.toList());
    }
    
    public List<Artist> artistsLivingInLondon() {
        return artists.stream()
                      .filter(artist -> "UK".equals(artist.getNationality()))
                      .collect(Collectors.toList());
    }


    public List<Artist> artistsLivingInChina() {
        return artists.stream()
                .filter(artist -> "CN".equals(artist.getNationality()))
                .collect(Collectors.toList());
    }


    /**
     * 分析
     * */
    public List<Artist> artistsLivingInChina_a() {
        return artists.stream()
                .filter(new Predicate<Artist>() {
                    @Override
                    public boolean test(Artist artist) {
                        return "CN".equals(artist.getNationality());
                    }
                })
                .collect(Collectors.toList());
    }

    /**
     * 分析
     *
     * stream必须后面加上所谓的terminal operation，类似于count()、forEach()、collect()等操作之后，才会消费原始数据流。
     *
     * */
    public List<String> getNamesOfArtists_Lambda_a(){

        List<Object> collect = super.artists.stream().map(new Function<Artist, String>() {
            @Override
            public String apply(Artist artist) {

                return  artist.getName();
            }
        }).collect(Collectors.toList());

        System.out.println(collect.getClass().getName());

        return null;
    }





    public static void main(String[] args){


        Chapter1 chapter1 = new Chapter1();
        System.out.println(chapter1.artists);
        System.out.println(chapter1.getNamesOfArtists_Lambda());
        System.out.println(chapter1.getNamesOfArtists_MethodReference());
        System.out.println(chapter1.artistsLivingInChina());

        List<String> collected = Stream.of("a", "b", "c").collect(Collectors.toList());


        System.out.println(collected.getClass().getName());
    }

}
